package cn.yeyiu.videoplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import java.util.List;

import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.bean.MangaEP;
import cn.yeyiu.util.bean.VideoDetail;
import cn.yeyiu.util.bean.VideoInfo;

public class VideoDetailActivity extends AppCompatActivity {

    private TextView headTitle;
    private TextView bgTitle;
    private TextView epDesc;

    private ImageView backIV;
    private ImageView epBgIV;

    private GridLayout epsGL;

    private VideoDetail videoDetail;
    private List<VideoInfo> videoInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);

        initData();
        bindView();
        bindEvent();
        initUI();
        inflateEps();
    }

    void initData() {
        this.videoDetail = JSONObject.parseObject(getIntent().getStringExtra("video_detail"), VideoDetail.class);
        this.videoInfos = videoDetail.eps;
    }

    void bindView() {
        headTitle = findViewById(R.id.back_tv);
        bgTitle = findViewById(R.id.ep_bg_title);
        epDesc = findViewById(R.id.ep_desc);
        backIV = findViewById(R.id.back_btn);
        epBgIV = findViewById(R.id.ep_bg);
        epsGL = findViewById(R.id.video_eps_menu_gl);
    }

    void bindEvent() {
        backIV.setOnClickListener(v -> finish());
    }

    void initUI() {
        headTitle.setText(this.videoDetail.title);
        bgTitle.setText(this.videoDetail.title);
        epDesc.setText(this.videoDetail.description);
        Glide.with(this)
                .load(videoDetail.logo)
                .into(epBgIV);

    }

    void inflateEps() {
        TextView view;
        for (VideoInfo ep : videoInfos) {
            view = new TextView(this);
            view.setText(ep.title);
            view.setTextSize(26);
            view.setPadding(20, 20, 20, 10);
            epsGL.addView(view);

            view.setOnClickListener(v -> {
                if("web".equals(ep.info)){
                    AndroidUtil.openInBrowser(VideoDetailActivity.this, ep.videoSrc);
                }else{
                    MediaUtil.playVideo(VideoDetailActivity.this, VideoPlayerActivity.class, ep);
                }
            });
        }
    }
}